﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    /// <summary>
    /// Complete list of supported Currency Pairs.
    /// </summary>
    public enum CurrencyPair
    {

        EURUSD = 0,
        USDJPY = 1,
        GBPUSD = 2,
        USDCHF = 3,
        UNSUPPORTED = 4
    }


    /// <summary>
    /// General collection of shared support functions
    /// </summary>
    public static class Shared
    {
        /// <summary>
        /// Return filename in standard format for processing.  Call this before calling GetCurrencyPairFromFileName
        /// </summary>
        /// <param name="name"></param>
        /// <param name="include_extension"></param>
        /// <returns></returns>
        public static string NormalizeFileName(string name, bool include_extension)
        {
            string res = "";

            if (!include_extension)
            {
                string[] _b = name.Split('.');
                res = _b[0];
            }
            else
                res = name;

            res = res.ToUpper().Trim();

            return res;
        }

        /// <summary>
        /// Match a FileName to the CurrencyPair it contains.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static CurrencyPair GetCurrencyPairFromFileName(string name)
        {
            string[] _p = name.Split('_');
            string _pair = "";

            if (_p.Length >= 2)
            {
                _pair = _p[0] + _p[1];
            }

            if (_pair == "")
                return CurrencyPair.UNSUPPORTED;
            else
                return GetCurrencyPairID(NormalizeCurrencyPairName(_pair));
        }


        /// <summary>
        /// Return Currency Pair name in standard format so that it can be matched to the CurrencyPair enum
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string NormalizeCurrencyPairName(string name)
        {
            char[] arr = name.ToUpper().Replace(" ", "").Trim().ToCharArray();

            arr = Array.FindAll<char>(arr, (c => (char.IsLetterOrDigit(c) || char.IsWhiteSpace(c) || c == '-')));
            name = new string(arr);

            return name;
        }

        /// <summary>
        /// Return the CurrencyPair enum of the pair
        /// </summary>
        /// <param name="pair"></param>
        /// <returns></returns>
        public static CurrencyPair GetCurrencyPairID(string pair)
        {
            CurrencyPair res;

            switch (pair)
            {

                case ("EURUSD"):
                    res = CurrencyPair.EURUSD;
                    break;
                case ("USDJPY"):
                    res = CurrencyPair.USDJPY;
                    break;
                case ("GBPUSD"):
                    res = CurrencyPair.GBPUSD;
                    break;
                case ("USDCHF"):
                    res = CurrencyPair.USDCHF;
                    break;
                default:
                    res = CurrencyPair.UNSUPPORTED;
                    break;
            }

            return res;
        }
    }
}
